/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nls.speechhandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.nls.client.protocol.asr.SpeechTranscriberListener;
import com.alibaba.nls.client.protocol.asr.SpeechTranscriberResponse;
import com.component.NotifyObject;
import com.vo.ResultJson;

/**
 *
 * @author knight
 */
public class CustomSpeechTranscriberListener extends SpeechTranscriberListener {
    
	private static Logger LOG = LoggerFactory.getLogger(CustomSpeechTranscriberListener.class);
	
	//通知器
	private NotifyObject notifyObject;

    /**
	 * @param resultJson
	 */
	public CustomSpeechTranscriberListener(NotifyObject notifyObject) {
		this.notifyObject = notifyObject;
	}

	@Override
    public void onSentenceEnd(SpeechTranscriberResponse response) {
		LOG.info("最终结果---"+"name: " + response.getName()
                + // 状态码 20000000 表示正常识别
                ", status: " + response.getStatus()
                + // 句子编号，从1开始递增
                ", index: " + response.getTransSentenceIndex()
                + // 当前句子的完整识别结果
                ", result: " + response.getTransSentenceText()
                + // 当前已处理的音频时长，单位是毫秒
                ", time: " + response.getTransSentenceTime()
                + // SentenceBegin事件的时间，单位是毫秒
                ", begin time: " + response.getSentenceBeginTime()
                + // 识别结果置信度，取值范围[0.0, 1.0]，值越大表示置信度越高
                ", confidence: " + response.getConfidence());
		//使用传递结果
        ResultJson resultJson = notifyObject.getResultJson();
        resultJson.setCode(response.getStatus());
        resultJson.setResult(response.getTransSentenceText());
        //通知语音识别完成
    	notifyObject.notifyRecognized();
    }
	@Override
    public void onTranscriptionResultChange(SpeechTranscriberResponse response) {
    	LOG.info("中间结果---"+"name: " + response.getName() +
                 // 状态码 20000000 表示正常识别
                 ", status: " + response.getStatus() +
                 // 句子编号，从1开始递增
                 ", index: " + response.getTransSentenceIndex() +
                 // 当前句子的中间识别结果
                 ", result: " + response.getTransSentenceText() +
                 // 当前已处理的音频时长，单位是毫秒
                 ", time: " + response.getTransSentenceTime());
    }

    // 识别完毕
    @Override
    public void onTranscriptionComplete(SpeechTranscriberResponse response) {
    	LOG.info("name: " + response.getName()+ ", status: " + response.getStatus());
    }

}
